//
//  AlertPhoneViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 16/8/30.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "AlertPhoneViewController.h"
#import "ADCountButton.h"
#import "NSString+Check.h"

@interface AlertPhoneViewController ()<ADCountButtonDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textFieldPhone;
@property (weak, nonatomic) IBOutlet UITextField *textFieldCodes;
@property (weak, nonatomic) IBOutlet ADCountButton *btnCodes;

@end

@implementation AlertPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改手机号";
    [self showNavgationWithLeftItemWithTitle:@"返回"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _btnCodes.delegate = self;
    _textFieldCodes.keyboardType  = _textFieldPhone.keyboardType = UIKeyboardTypePhonePad;

    
}

#pragma mark - 
- (void)getVerificationCode:(NSString *)verificationCode{
    [SVProgressHUD showWithStatus:@"获取验证码..." maskType:SVProgressHUDMaskTypeClear];
    [[BaseDataRequest alloc] getVerificationCode:verificationCode WithBlock:^(id responseObject, NSString *status, NSError *error) {
        [SVProgressHUD dismiss];
        if ([status isEqualToString:@"0"]) {
            [SVProgressHUD showSuccessWithStatus:@"验证码已发送至您的手机!"];
        }else{
            if (error != nil) {
                TIPSFORVIEW(@"请检测网络设置");
            }else{
                TIPSFORVIEW(@"获取验证码失败");
            }
        }
    }];
}

- (IBAction)changePhone:(id)sender {
    if (_textFieldPhone.text.length == 0) {
        TIPSFORVIEW(@"手机号不能为空！");
        return;
    }
    if (![_textFieldPhone.text checkPhoneNumInput]) {
        return;
    }
    if (_textFieldCodes.text.length == 0) {
        TIPSFORVIEW(@"验证码不能为空！");
        return;
    }
    [SVProgressHUD showWithStatus:@"修改手机号..." maskType:SVProgressHUDMaskTypeClear];
    [[BaseDataRequest alloc] changeBindYourPhone:_textFieldPhone.text code:_textFieldCodes.text withBlock:^(id responseObject, NSString *status, NSError *error) {
        [SVProgressHUD dismiss];
        if ([status isEqualToString:@"0"]) {
            //[SVProgressHUD showSuccessWithStatus:@"修改手机号成功!"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            if (error != nil) {
                TIPSFORVIEW(@"请检测网络设置");
            }else{
                TIPSFORVIEW([responseObject objectForKey:@"message"]);
            }
        }
    }];
}

- (IBAction)getCodes:(id)sender {
    if (_textFieldPhone.text.length == 0) {
        TIPSFORVIEW(@"手机号不能为空！");
        return;
    }
    if (![_textFieldPhone.text checkPhoneNumInput]) {
        TIPSFORVIEW(@"手机号格式错误！");
        return;
    }
    [self getVerificationCode:_textFieldPhone.text];
    [self.btnCodes startCountDown];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
